package cn.livingCloud.controller.core;

import cn.livingCloud.dto.ReportRuleFormDto;
import cn.livingCloud.dto.ReportRuleListDto;
import cn.livingCloud.service.ReportRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * Created by DingJie on 2016/6/27.
 */
@Controller
@RequestMapping("/admin/report/report_rule")
public class ReportRuleController {
    @Autowired
    private ReportRuleService service;

    @RequestMapping("/list.htm")
    public String list(ReportRuleListDto listDto, Model model) {
        service.loadReportRuleListDto(listDto);
        model.addAttribute("listDto", listDto);
        return "report/report_rule/list";
    }

    @RequestMapping("/edit.htm")

    public String edit(ReportRuleFormDto formDto, Model model) {
        service.loadReportRuleFormDto(formDto);
        model.addAttribute("formDto", formDto);
        return "report/report_rule/edit";
    }

    @RequestMapping(value = "/edit.htm", method = RequestMethod.POST)
    public String edit(@ModelAttribute("formDto") @Valid ReportRuleFormDto formDto, BindingResult result) {
        if (result.hasErrors()) {
            return "report/report_rule/edit";
        }
        service.edit(formDto);
        return "redirect:list.htm?edit=success";
    }

    //删除
    @RequestMapping(value = "/delete.htm", method = RequestMethod.POST)
    public String delete(String uuid, Model model) {
        service.delete(uuid);
        model.addAttribute("delete", "success");
        return "redirect:list.htm";
    }

    //预览
    @RequestMapping("/preview.htm")
    public String preview(String uuid, HttpServletRequest request, Model model) {
        String previewDate = service.loadsPreviewDate(uuid, request);
        model.addAttribute("previewDate", previewDate);
        return "report/report_rule/preview";
    }

    //查看内容
    @RequestMapping("/content.htm")
    public String showContent(@RequestParam("uuid") String uuid, Model model) {
        String reportRuleText = service.loadContent(uuid);
        model.addAttribute("reportRuleText", reportRuleText);
        return "report/report_rule/content";
    }
}
